package com.stu.student_back_end.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stu.student_back_end.common.Constants;
import com.stu.student_back_end.common.Result;
import com.stu.student_back_end.controller.dto.CountDTO;
import com.stu.student_back_end.controller.dto.LoginDTO;
import com.stu.student_back_end.controller.dto.PageDTO;
import com.stu.student_back_end.entity.TblMoveAndOut;
import com.stu.student_back_end.entity.TblNoHere;
import com.stu.student_back_end.entity.TblStudent;
import com.stu.student_back_end.mapper.TblStudentMapper;
import com.stu.student_back_end.service.ITblMoveAndOutService;
import com.stu.student_back_end.service.ITblNoHereService;
import com.stu.student_back_end.service.ITblStudentService;
import com.stu.student_back_end.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *学生service层实体类
 */

@Service
public class TblStudentServiceImpl extends ServiceImpl<TblStudentMapper, TblStudent> implements ITblStudentService {

    @Autowired
    private ITblMoveAndOutService moveAndOutService;

    @Autowired
    private ITblNoHereService noHereService;

    @Override
    public Result login(String username, String password) {
        //查询账号
        TblStudent student = getOne(new LambdaQueryWrapper<TblStudent>().eq(TblStudent::getSnoId, username));
        //查询到账号
        if (student != null) {
            //校验密码密码
            if(student.getPassword().equals(password)) {
                String id = student.getId() + "";
                String token = TokenUtil.createToken(id,student.getPassword());
                LoginDTO loginDTO = new LoginDTO();
                loginDTO.setToken(token);
                loginDTO.setStudent(student);
                loginDTO.setUserType("学生");
                TokenUtil.setToken(token);
                return Result.success("登录成功",loginDTO);
            }else {
                return Result.error(Constants.CODE_600,"密码错误");
            }
        }else {
            return Result.error(Constants.CODE_600,"账号错误");
        }
    }

    @Override
    public Page<TblStudent> queryStudent(PageDTO pageDTO) {
        Page<TblStudent> page =new Page<>(pageDTO.getCurrentPage(),pageDTO.getSizePage());
        Page<TblStudent> studentPage = new Page<>();
        //判断是否带关键字
        if(!(pageDTO.getKeyWord() == "")) {
            //不为空
            studentPage = page(page, new LambdaQueryWrapper<TblStudent>().like(TblStudent::getName, pageDTO.getKeyWord()));
        }else {
            studentPage = page(page);
        }
        return studentPage;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public boolean updateStudentState(TblMoveAndOut moveAndOutVo, int id) {
        //判断state是否为null
        if(moveAndOutVo.getState() == 1) {
            moveAndOutVo.setState(0);
        }else {
            moveAndOutVo.setState(1);
        }


        int update = baseMapper.update(null, new LambdaUpdateWrapper<TblStudent>()
                .eq(TblStudent::getId, id)
                .set(TblStudent::getState, moveAndOutVo.getState()));

        //调用方法插入记录
        int i = moveAndOutService.saveMoveAndOut(moveAndOutVo);

        if(update != 1 || i != 1) throw new RuntimeException();

        return true;
    }

    @Override
    public CountDTO queryStudentCount() {
        CountDTO countVo = new CountDTO();
        countVo.setName("学生");
        countVo.setCount(baseMapper.queryStudentCount());
        return countVo;
    }

    @Override
    public boolean addStudentNoHere(TblNoHere noHere) {
        return noHereService.save(noHere);
    }
}
